﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using System.IO;

namespace 获取坐标
{
    public partial class Form1 : Form
    {
        AcadApplication AcadApp;
        AcadDocument AcadDoc;

        public Form1()
        {
            InitializeComponent();
        }

        private void 启动CAD()
        {
            try
            {
                AcadApp =
               (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application");
                AcadDoc = AcadApp.ActiveDocument;
            }
            catch
            {
                OpenFileDialog pOpenDG = new OpenFileDialog();
                pOpenDG.Filter = "CAD文件(*.dwg)|*.dwg|CAD图形文件(*.dxf)|*.dxf";
                pOpenDG.Title = "打开CAD文件";
                pOpenDG.ShowDialog();
                string filePath = pOpenDG.FileName;
                if (filePath == "")
                {
                    MessageBox.Show("选择CAD文件无效！", "文件无效！");
                    Application.Exit();
                }
                AcadApp = new AcadApplication();
                AcadDoc = AcadApp.Documents.Open(filePath, null, null);
            }
            AcadApp.Application.Visible = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            启动CAD();
        }

        private void GetLineCoords(AcadLine lineObj, out double[] coordsX, out double[] coordsY, out
double[] coordsZ, int arrayCount)
        {
            coordsX = new double[2 * arrayCount];
            coordsY = new double[2 * arrayCount];
            coordsZ = new double[2 * arrayCount];
            coordsX[2 * arrayCount - 2] = ((double[])lineObj.StartPoint)[0];
            coordsX[2 * arrayCount - 1] = ((double[])lineObj.EndPoint)[0];
            coordsY[2 * arrayCount - 2] = ((double[])lineObj.StartPoint)[1];
            coordsY[2 * arrayCount - 1] = ((double[])lineObj.EndPoint)[1];
            coordsZ[2 * arrayCount - 2] = ((double[])lineObj.StartPoint)[2];
            coordsZ[2 * arrayCount - 1] = ((double[])lineObj.EndPoint)[2];
        }

        private void 获取直线坐标_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet001");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] resultX, resultY, resultZ;
            int count = 0;
            string showInfoText = "";
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbLine")
                {
                    count++;
                    resultX = new Double[2 * count];
                    resultY = new Double[2 * count];
                    resultZ = new Double[2 * count];
                    GetLineCoords((AcadLine)acadObj, out resultX, out resultY, out resultZ, count);
                    showInfoText += resultX[2 * count - 2].ToString() + "," + resultY[2 * count -
2].ToString() + "," + resultZ[2 * count - 2].ToString() + "\n"
 + resultX[2 * count - 1].ToString() + "," + resultY[2 * count - 1].ToString()
+ "," + resultZ[2 * count - 1].ToString() + "\n";
                }
            }
            InfoRichTextBox.Text = showInfoText;
            AcadDoc.SelectionSets.Item("NewSelectionSet001").Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void GetPolylineCoords(AcadLWPolyline polylineObj, out double[] coordsX, out double[]
coordsY, out double[] coordsZ, int arrayCount)
        {
            double[] polylineCoords;
            polylineCoords = (double[])polylineObj.Coordinates;
            coordsX = new double[arrayCount];
            coordsY = new double[arrayCount];
            coordsZ = new double[arrayCount];
            int tempInt = arrayCount - polylineCoords.Length / 2;
            for (int i = 0; i < polylineCoords.Length / 2; i++)
            {
                coordsX[i + tempInt] = polylineCoords[2 * i];
                coordsY[i + tempInt] = polylineCoords[2 * i + 1];
                coordsZ[i + tempInt] = polylineObj.Elevation;
            }
        }

        private void 获取多段线节点坐标_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet002");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] resultX, resultY, resultZ;
            int i, count = 0;
            string showInfoText = "";
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbPolyline")
                {
                    int tempCount = count;
                    count += ((double[])((AcadLWPolyline)acadObj).Coordinates).Length / 2;
                    resultX = new Double[count];
                    resultY = new Double[count];
                    resultZ = new Double[count];
                    GetPolylineCoords((AcadLWPolyline)acadObj, out resultX, out resultY, out resultZ,
                   count);
                    for (i = tempCount; i < count; i++)
                    {
                        showInfoText += resultX[i].ToString() + "," + resultY[i].ToString() + "," +
resultZ[i].ToString() + "\n";
                    }
                }
            }
            InfoRichTextBox.Text = showInfoText;
            AcadDoc.SelectionSets.Item("NewSelectionSet002").Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void Get3DPolylineCoords(Acad3DPolyline TDPolylineObj, out double[] coordsX, out
double[] coordsY, out double[] coordsZ, int arrayCount)
        {
            double[] TDpolylineCoords;
            TDpolylineCoords = (double[])TDPolylineObj.Coordinates;
            coordsX = new double[arrayCount];
            coordsY = new double[arrayCount];
            coordsZ = new double[arrayCount];
            int tempInt = arrayCount - TDpolylineCoords.Length / 3;
            for (int i = 0; i < TDpolylineCoords.Length / 3; i++)
            {
                coordsX[i + tempInt] = TDpolylineCoords[3 * i];
                coordsY[i + tempInt] = TDpolylineCoords[3 * i + 1];
                coordsZ[i + tempInt] = TDpolylineCoords[3 * i + 2];
            }
        }

        private void 获取三维多段线节点坐标_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet003");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] resultX, resultY, resultZ;
            int i, count = 0;
            string showInfoText = "";
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDb3dPolyline")
                {
                    int tempCount = count;
                    count += ((double[])((Acad3DPolyline)acadObj).Coordinates).Length / 3;
                    resultX = new Double[count];
                    resultY = new Double[count];
                    resultZ = new Double[count];
                    Get3DPolylineCoords((Acad3DPolyline)acadObj, out resultX, out resultY, out
                   resultZ, count);
                    for (i = tempCount; i < count; i++)
                    {
                        showInfoText += resultX[i].ToString() + "," + resultY[i].ToString() + "," +
                       resultZ[i].ToString() + "\n";
                    }
                }
            }
            InfoRichTextBox.Text = showInfoText;
            AcadDoc.SelectionSets.Item("NewSelectionSet003").Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void GetSplineControlCoords(AcadSpline splineObj, out double[] coordsX, out double[]
coordsY, out double[] coordsZ, int arrayCount)
        {
            double[] splineCoords;
            splineCoords = (double[])splineObj.ControlPoints;
            coordsX = new double[arrayCount];
            coordsY = new double[arrayCount];
            coordsZ = new double[arrayCount];
            int tempInt = arrayCount - splineCoords.Length / 3;
            for (int i = 0; i < splineCoords.Length / 3; i++)
            {
                coordsX[i + tempInt] = splineCoords[3 * i];
                coordsY[i + tempInt] = splineCoords[3 * i + 1];
                coordsZ[i + tempInt] = splineCoords[3 * i + 2];
            }
        }

        private void 获取样条曲线控制点坐标_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet004");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] resultX, resultY, resultZ;
            int i, count = 0;
            string showInfoText = "";
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbSpline")
                {
                    int tempCount = count;
                    count += ((double[])((AcadSpline)acadObj).ControlPoints).Length / 3;
                    resultX = new Double[count];
                    resultY = new Double[count];
                    resultZ = new Double[count];
                    GetSplineControlCoords((AcadSpline)acadObj, out resultX, out resultY, out
                   resultZ, count);
                    for (i = tempCount; i < count; i++)
                    {
                        showInfoText += resultX[i].ToString() + "," + resultY[i].ToString() + "," +
                       resultZ[i].ToString() + "\n";
                    }
                }
            }
            InfoRichTextBox.Text = showInfoText;
            AcadDoc.SelectionSets.Item("NewSelectionSet004").Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void GetSplineFitCoords(AcadSpline splineObj, out double[] coordsX, out double[]
coordsY, out double[] coordsZ, int arrayCount)
        {
            double[] splineCoords;
            splineCoords = (double[])splineObj.FitPoints;
            coordsX = new double[arrayCount];
            coordsY = new double[arrayCount];
            coordsZ = new double[arrayCount];
            int tempInt = arrayCount - splineCoords.Length / 3;
            for (int i = 0; i < splineCoords.Length / 3; i++)
            {
                coordsX[i + tempInt] = splineCoords[3 * i];
                coordsY[i + tempInt] = splineCoords[3 * i + 1];
                coordsZ[i + tempInt] = splineCoords[3 * i + 2];
            }
        }

        private void 获取样条曲线拟合点坐标_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet005");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] resultX, resultY, resultZ;
            int i, count = 0;
            string showInfoText = "";
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbSpline")
                {
                    int tempCount = count;
                    count += ((double[])((AcadSpline)acadObj).FitPoints).Length / 3;
                    resultX = new Double[count];
                    resultY = new Double[count];
                    resultZ = new Double[count];
                    GetSplineFitCoords((AcadSpline)acadObj, out resultX, out resultY, out resultZ,
                   count);
                    for (i = tempCount; i < count; i++)
                    {
                        showInfoText += resultX[i].ToString() + "," + resultY[i].ToString() + "," +
                       resultZ[i].ToString() + "\n";
                    }
                }
            }
            InfoRichTextBox.Text = showInfoText;
            AcadDoc.SelectionSets.Item("NewSelectionSet005").Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 获取点的坐标_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet006");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] resultX, resultY, resultZ;
            int count = 0;
            string showInfoText = "";
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbPoint")
                {
                    count++;
                    resultX = new Double[count];
                    resultY = new Double[count];
                    resultZ = new Double[count];
                    double[] PointCoord;
                    PointCoord = (Double[])(((AcadPoint)acadObj).Coordinates);
                    resultX[count - 1] = PointCoord[0];
                    resultY[count - 1] = PointCoord[1];
                    resultZ[count - 1] = PointCoord[2];
                    showInfoText += resultX[count - 1].ToString() + "," + resultY[count - 1].ToString()
                   + "," + resultZ[count - 1].ToString() + "\n";
                }
            }
            InfoRichTextBox.Text = showInfoText;
            AcadDoc.SelectionSets.Item("NewSelectionSet006").Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 获取块的插入点_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet007");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] resultX, resultY, resultZ;
            int count = 0;
            string showInfoText = "";
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbBlockReference")
                {
                    count++;
                    resultX = new Double[count];
                    resultY = new Double[count];
                    resultZ = new Double[count];
                    double[] insertPointCoord;
                    insertPointCoord = (Double[])(((AcadBlockReference)acadObj).InsertionPoint);
                    resultX[count - 1] = insertPointCoord[0];
                    resultY[count - 1] = insertPointCoord[1];
                    resultZ[count - 1] = insertPointCoord[2];
                    showInfoText += resultX[count - 1].ToString() + "," + resultY[count - 1].ToString()
                   + "," + resultZ[count - 1].ToString() + "\n";
                }
            }
            InfoRichTextBox.Text = showInfoText;
            AcadDoc.SelectionSets.Item("NewSelectionSet007").Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 获得文字插入点及内容_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet008");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] resultX, resultY, resultZ;
            String[] textArray;
            int count = 0;
            string showInfoText = "";
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbText")
                {
                    count++;
                    resultX = new Double[count];
                    resultY = new Double[count];
                    resultZ = new Double[count];
                    textArray = new String[count];
                    double[] insertPointCoord;
                    insertPointCoord = (Double[])(((AcadText)acadObj).InsertionPoint);
                    resultX[count - 1] = insertPointCoord[0];
                    resultY[count - 1] = insertPointCoord[1];
                    resultZ[count - 1] = insertPointCoord[2];
                    textArray[count - 1] = ((AcadText)acadObj).TextString;
                    showInfoText += resultX[count - 1].ToString() + "," + resultY[count - 1].ToString()
                   + "," + resultZ[count - 1].ToString()
                    + "," + textArray[count - 1].ToString() + "\n";
                }
                else if (acadObj.ObjectName == "AcDbMText")
                {
                    count++;
                    resultX = new Double[count];
                    resultY = new Double[count];
                    resultZ = new Double[count];
                    textArray = new String[count];
                    double[] insertPointCoord;
                    insertPointCoord = (Double[])(((AcadMText)acadObj).InsertionPoint);
                    resultX[count - 1] = insertPointCoord[0];
                    resultY[count - 1] = insertPointCoord[1];
                    resultZ[count - 1] = insertPointCoord[2];
                    textArray[count - 1] = ((AcadMText)acadObj).TextString;
                    showInfoText += resultX[count - 1].ToString() + "," + resultY[count - 1].ToString()
+ "," + resultZ[count - 1].ToString()
 + "," + textArray[count - 1].ToString() + "\n";
                }
            }
            InfoRichTextBox.Text = showInfoText;
            AcadDoc.SelectionSets.Item("NewSelectionSet008").Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 获取各种线节点坐标_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet009");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] resultX, resultY, resultZ;
            int i, count = 0;
            string showInfoText = "";
            dataGridView.Rows.Clear();
            dataGridView.Columns.Add("", "序号");
            dataGridView.Columns.Add("", "X");
            dataGridView.Columns.Add("", "Y");
            dataGridView.Columns.Add("", "Z");
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbLine")
                {
                    count++;
                    resultX = new Double[2 * count];
                    resultY = new Double[2 * count];
                    resultZ = new Double[2 * count];
                    GetLineCoords((AcadLine)acadObj, out resultX, out resultY, out resultZ, count);
                    showInfoText += resultX[2 * count - 2].ToString() + "," + resultY[2 * count -
                   2].ToString() + "," + resultZ[2 * count - 2].ToString() + "\n"
                    + resultX[2 * count - 1].ToString() + "," + resultY[2 * count - 1].ToString()
                   + "," + resultZ[2 * count - 1].ToString() + "\n";

                    dataGridView.Rows.Add(1);
                    dataGridView.Rows[count - 1].Cells[0].Value = count;
                    dataGridView.Rows[count - 1].Cells[1].Value = resultX[count - 1];
                    dataGridView.Rows[count - 1].Cells[2].Value = resultY[count - 1];
                    dataGridView.Rows[count - 1].Cells[3].Value = resultZ[count - 1];
                }
                else if (acadObj.ObjectName == "AcDbPolyline")
                {
                    int tempCount = count;
                    count += ((double[])((AcadLWPolyline)acadObj).Coordinates).Length / 2;
                    resultX = new Double[count];
                    resultY = new Double[count];
                    resultZ = new Double[count];
                    GetPolylineCoords((AcadLWPolyline)acadObj, out resultX, out resultY, out resultZ,
                   count);
                    for (i = tempCount; i < count; i++)
                    {
                        showInfoText += resultX[i].ToString() + "," + resultY[i].ToString() + "," +
                       resultZ[i].ToString() + "\n";
                        dataGridView.Rows.Add(1);
                        dataGridView.Rows[i].Cells[0].Value = i + 1;
                        dataGridView.Rows[i].Cells[1].Value = resultX[i];
                        dataGridView.Rows[i].Cells[2].Value = resultY[i];
                        dataGridView.Rows[i].Cells[3].Value = resultZ[i];
                    }
                }
                else if (acadObj.ObjectName == "AcDb3dPolyline")
                {
                    int tempCount = count;
                    count += ((double[])((Acad3DPolyline)acadObj).Coordinates).Length / 3;
                    resultX = new Double[count];
                    resultY = new Double[count];
                    resultZ = new Double[count];
                    Get3DPolylineCoords((Acad3DPolyline)acadObj, out resultX, out resultY, out
                   resultZ, count);
                    for (i = tempCount; i < count; i++)
                    {
                        showInfoText += resultX[i].ToString() + "," + resultY[i].ToString() + "," +
                       resultZ[i].ToString() + "\n";
                        dataGridView.Rows.Add(1);
                        dataGridView.Rows[i].Cells[0].Value = i + 1;
                        dataGridView.Rows[i].Cells[1].Value = resultX[i];
                        dataGridView.Rows[i].Cells[2].Value = resultY[i];
                        dataGridView.Rows[i].Cells[3].Value = resultZ[i];
                    }
                }
                else if (acadObj.ObjectName == "AcDbSpline")
                {
                    int tempCount = count;
                    count += ((double[])((AcadSpline)acadObj).ControlPoints).Length / 3;
                    resultX = new Double[count];
                    resultY = new Double[count];
                    resultZ = new Double[count];
                    GetSplineControlCoords((AcadSpline)acadObj, out resultX, out resultY, out
                   resultZ, count);
                    for (i = tempCount; i < count; i++)
                    {
                        showInfoText += resultX[i].ToString() + "," + resultY[i].ToString() + "," +
                            resultZ[i].ToString() + "\n";
                        dataGridView.Rows.Add(1);
                        dataGridView.Rows[i].Cells[0].Value = i + 1;
                        dataGridView.Rows[i].Cells[1].Value = resultX[i];
                        dataGridView.Rows[i].Cells[2].Value = resultY[i];
                        dataGridView.Rows[i].Cells[3].Value = resultZ[i];
                    }
                }
            }
            InfoRichTextBox.Text = showInfoText;
            AcadDoc.SelectionSets.Item("NewSelectionSet009").Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 保存为文本文档_Click(object sender, EventArgs e)
        {
            handle01:
            SaveFileDialog saveTextDg = new SaveFileDialog();
            saveTextDg.Title = "保存为文本文件";
            saveTextDg.Filter = "文本文件(*.txt)|*.txt|文本文件(*.dat)|*.dat";
            saveTextDg.ShowDialog();
            string filePath = saveTextDg.FileName;
            if (filePath == "")
            {
                MessageBox.Show("保存路径错误！");
                goto handle01;
            }
            using (StreamWriter sw = File.CreateText(filePath))
            {
                sw.WriteLine(InfoRichTextBox.Text);
                sw.Close();
            }
        }
    }
}
